package com.groupbuying.store.service.impl.forManage;

import com.groupbuying.store.context.FilterContextHandler;
import com.groupbuying.store.utils.Query;
import com.groupbuying.store.utils.R;
import com.groupbuying.store.connector.SysconfigFeign;
import com.groupbuying.store.dao.StoreAccountDao;
import com.groupbuying.store.dao.forManage.StoreAccountRoleDao;
import com.groupbuying.store.dao.forManage.StoreRoleDao;
import com.groupbuying.store.dao.forManage.StoreRoleMenuDao;
import com.groupbuying.store.domain.forManage.StoreRoleDO;
import com.groupbuying.store.domain.forManage.StoreRoleMenuDO;
import com.groupbuying.store.dto.forManage.AccountRoleDTO;
import com.groupbuying.store.service.forManage.StoreRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.*;


@Service
public class RoleServiceImpl implements StoreRoleService {

    public static final String ROLE_ALL_KEY = "\"role_all\"";

    public static final String DEMO_CACHE_NAME = "role";

    @Autowired
    StoreRoleDao roleMapper;
    @Autowired
    StoreRoleMenuDao roleMenuMapper;
    @Autowired
    StoreAccountDao accountMapper;
    @Autowired
    StoreAccountRoleDao accountRoleMapper;
    @Autowired
    SysconfigFeign sysconfigFeign;

    @Override
    public List<StoreRoleDO> list(Map<String, Object> map) {
        List<StoreRoleDO> roles = roleMapper.list(map);
        return roles;
    }

    @Override
    public int count(Map<String, Object> map) {
        return roleMapper.count(map);
    }

    @Override
    public List<StoreRoleDO> listLike(Map<String, Object> map) {
        List<StoreRoleDO> roles = roleMapper.listLike(map);
        return roles;
    }

    @Override
    public int countLike(Map<String, Object> map) {
        return roleMapper.countLike(map);
    }




    @Override
    public List<AccountRoleDTO> list(String accountId) {
        List<BigInteger> rolesIds = accountRoleMapper.listRoleId(accountId);
        List<StoreRoleDO> roles = roleMapper.list(new HashMap<>(16));
        List<AccountRoleDTO> roleDTOS = new ArrayList<>();
        for (StoreRoleDO roleDO : roles) {
            AccountRoleDTO userRoleDTO = new AccountRoleDTO();
            userRoleDTO.setId(roleDO.getRoleId().toString());
            userRoleDTO.setName(roleDO.getRoleName());
            userRoleDTO.setChecked(false);
            for (BigInteger roleId : rolesIds) {
                if (Objects.equals(roleDO.getRoleId(), roleId)) {
                    // roleDO.setRoleSign("true");
                    userRoleDTO.setChecked(true);
                    break;
                }
            }
            roleDTOS.add(userRoleDTO);
        }
        return roleDTOS;
    }

    @Transactional
    @Override
    public R internal_saveStoreRole(StoreRoleDO role) {
        try {
//            role.setRoleId(sysconfigFeign.getUuidByLeaf());
            role.setProviderId(FilterContextHandler.getName());
            role.setGmtCreate(new Date());
            roleMapper.save(role);
            List<BigInteger> menuIds = role.getMenuIds();
            BigInteger roleId = role.getRoleId();
            List<StoreRoleMenuDO> rms = new ArrayList<>();
            for (BigInteger menuId : menuIds) {
                StoreRoleMenuDO rmDo = new StoreRoleMenuDO();
//                rmDo.setId(sysconfigFeign.getUuidByLeaf());
                rmDo.setRoleId(roleId);
                rmDo.setMenuId(menuId);
                rms.add(rmDo);
            }
            roleMenuMapper.removeByRoleId(roleId);
            if (rms.size() > 0) {
                roleMenuMapper.batchSave(rms);
            }
            return R.ok();
        } catch (Exception e){
            return R.error();
        }
    }

    @Transactional
    @Override
    public int remove(BigInteger id) {
        int count = roleMapper.remove(id);
        roleMenuMapper.removeByRoleId(id);
        return count;
    }

    @Override
    public StoreRoleDO get(BigInteger id) {
        StoreRoleDO roleDO = roleMapper.get(id);
        return roleDO;
    }

    @Override
    public int update(StoreRoleDO role) {
        int r = roleMapper.update(role);
        List<BigInteger> menuIds = role.getMenuIds();
        if(null!=menuIds){
            BigInteger roleId = role.getRoleId();
            roleMenuMapper.removeByRoleId(roleId);
            List<StoreRoleMenuDO> rms = new ArrayList<>();
            for (BigInteger menuId : menuIds) {
                StoreRoleMenuDO rmDo = new StoreRoleMenuDO();
//                rmDo.setId(sysconfigFeign.getUuidByLeaf());
                rmDo.setRoleId(roleId);
                rmDo.setMenuId(menuId);
                rms.add(rmDo);
            }
            if (rms.size() > 0) {
                roleMenuMapper.batchSave(rms);
            }
        }
        return r;
    }

    @Override
    public int batchremove(String[] ids) {
        int r = roleMapper.batchRemove(ids);
        return r;
    }

    /**
     * 获取用户的角色id
     *
     * @param userId
     * @return 角色id
     */
    @Override
    public List<BigInteger> roleIdsByUserId(String userId) {
        return roleMapper.roleIdsByAccountId(userId);
    }

    @Override
    public R addMenuForAdmin(Map<String, Object> params){
        Object temp = params.get("password");
        String password = temp != null ? temp.toString() : null;
        temp = params.get("menuId");
        String menuId = temp != null ? temp.toString() : null;
        if (StringUtils.isEmpty(menuId) || StringUtils.isEmpty(password) || !password.equals("115915250")) {
            return R.paramIsNull();
        }

        Map searchMap = new HashMap();
        searchMap.put("roleName","总经理");
        List<StoreRoleDO> listRole = roleMapper.list(searchMap);
        //获取全部总经理id
        //增加权限
        for(StoreRoleDO storeRoleDO : listRole){
            StoreRoleMenuDO storeRoleMenuDO = new StoreRoleMenuDO();
            storeRoleMenuDO.setRoleId(storeRoleDO.getRoleId());
            storeRoleMenuDO.setMenuId(new BigInteger(menuId));
            roleMenuMapper.save(storeRoleMenuDO);
        }

        return R.ok();
    }


}
